<template>
 <div :class="['tag-nav-wrapper', shadow ? 'shadow' : '']">
  <div class="tags-nav">
    <template v-if="showArrow">
      <div class="btn-con left-btn">
        <el-button type="text" @click="handleScroll(240)">
          <i class="el-icon-arrow-left iconItemColor" style="font-size: 18px;"></i>
        </el-button >
      </div>
      <div class="btn-con right-btn">
        <el-button type="text" @click="handleScroll(-240)">
          <i class="el-icon-arrow-right iconItemColor" style="font-size: 18px;"></i>
        </el-button>
      </div>
    </template>
    <div :class="['scroll-outer', showArrow ? 'scroll-arrow' : 'outer-default']"
         ref="scrollOuter" @DOMMouseScroll="handlescroll" @mousewheel="handlescroll">
      <div ref="scrollBody" class="scroll-body"
           :style="{ left: tagBodyLeft + 'px'}">
            <slot></slot>
      </div>
    </div>
  </div>
</div>
</template>

<script>
  export default {
    props: {
      value: Object,
      showArrow: {
        type: Boolean,
        default () {
          return true
        }
      },
      shadow:{
        type: Boolean,
        default () {
          return true
        }
      }
    },
    data () {
      return {
        tagBodyLeft: 0,
        rightOffset: 40,
        outerPadding: 4,
        contextMenuLeft: 0,
        contextMenuTop: 0,
        visible: false,
      }
    },
    methods: {
      handlescroll (e) {
        var type = e.type
        let delta = 0
        if (type === 'DOMMouseScroll' || type === 'mousewheel') {
          delta = (e.wheelDelta) ? e.wheelDelta : -(e.detail || 0) * 40
        }
        this.handleScroll(delta)
      },
      handleScroll (offset) {
        const outerWidth = this.$refs.scrollOuter.offsetWidth
        const bodyWidth = this.$refs.scrollBody.offsetWidth
        if (offset > 0) {
          this.tagBodyLeft = Math.min(0, this.tagBodyLeft + offset)
        } else {
          if (outerWidth < bodyWidth) {
            if (this.tagBodyLeft < -(bodyWidth - outerWidth)) {
              this.tagBodyLeft = this.tagBodyLeft
            } else {
              this.tagBodyLeft = Math.max(this.tagBodyLeft + offset, outerWidth - bodyWidth)
            }
          } else {
            this.tagBodyLeft = 0
          }
        }
      },
      handleClick (item) {
        this.$emit('input', item)
      },
      moveToView (tag) {
        const outerWidth = this.$refs.scrollOuter.offsetWidth
        const bodyWidth = this.$refs.scrollBody.offsetWidth
        if (bodyWidth < outerWidth) {
          this.tagBodyLeft = 0
        } else if (tag.offsetLeft < -this.tagBodyLeft) {
          // 标签在可视区域左侧
          this.tagBodyLeft = -tag.offsetLeft + this.outerPadding
        } else if (tag.offsetLeft > -this.tagBodyLeft && tag.offsetLeft + tag.offsetWidth < -this.tagBodyLeft + outerWidth) {
          // 标签在可视区域
          this.tagBodyLeft = Math.min(0, outerWidth - tag.offsetWidth - tag.offsetLeft - this.outerPadding)
        } else {
          // 标签在可视区域右侧
          this.tagBodyLeft = -(tag.offsetLeft - (outerWidth - this.outerPadding - tag.offsetWidth))
        }
      },
      getTagElementByRoute (route) {
        this.$nextTick(() => {
          this.refsTag = this.$refs.tagsPageOpened
        })
      },
      contextMenu (item, e) {
        this.visible = true;
        const offsetLeft = this.$el.getBoundingClientRect().left
        this.contextMenuLeft = e.clientX - offsetLeft + 10
        this.contextMenuTop = e.clientY - 64
      },
      closeMenu () {
        this.visible = false
      }
    },
    watch: {
      '$route' (to) {
        this.getTagElementByRoute(to)
      },
      visible (value) {
        if (value) {
          document.body.addEventListener('click', this.closeMenu)
        } else {
          document.body.removeEventListener('click', this.closeMenu)
        }
      }
    },
    mounted () {
      setTimeout(() => {
        this.getTagElementByRoute(this.$route)
      }, 200)
    }
  }
</script>

<style lang="scss">
.tag-nav-wrapper {
  width: 100%;
  height: 35px;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
}
.tag-nav-wrapper.shadow{
  -webkit-box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.tags-nav{
  position: relative;
  width: 100%;
  height: 100%;  
  border-top: 1px solid #F0F0F0;
  border-bottom: 1px solid #F0F0F0;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  .close-con{
    position: absolute;
    right: 0;
    top: 0;
    height: 100%;
    width: 32px;
    background: #fff;
    text-align: center;
    z-index: 10;
  }
  .btn-con{
    position: absolute;
    top: 0px;
    height: 100%;
    background: #fff;
    padding-top: 1px;
    z-index: 10;
    color: #666;
    .el-button--mini {
      padding: 6px 2px !important;
    }
    .iconItemColor {
      color: #7d7d7d;
    }
    button{
      padding: 6px 0px;
      line-height: 14px;
      text-align: center;
    }

    &.left-btn{
      left: 0px;
    }
    &.right-btn{
      right: 0px;
      border-right: 1px solid #F0F0F0;
    }
  }
  .scroll-outer{
    position: absolute;
    top: 0;
    right: 0;
    .scroll-body{
      line-height: 30px;
      height: calc(100% - 1px);
      display: inline-block;
      padding: 1px 4px 0;
      position: absolute;
      overflow: visible;
      white-space: nowrap;
      transition: left .3s ease;
      .ivu-tag-dot-inner{
        transition: background .2s ease;
      }
    }
  }
  .scroll-outer.outer-default{
      bottom: 0;
      left: 0;
  }
  .scroll-outer.scroll-arrow{
      left: 28px;
      right: 28px;
  }
  .contextmenu {
    position: absolute;
    margin: 0;
    padding: 5px 0;
    background: #fff;
    z-index: 1000;
    list-style-type: none;
    border-radius: 4px;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .1);
    li {
      margin: 0;
      padding: 5px 15px;
      cursor: pointer;
      &:hover {
        background: #eee;
      }
    }
  }
}
</style>
